package com.adb.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.adb.entity.GameData;
import com.jjmmbb.baoSudoku.Comm;

public class GameDataManager {
	private ArrayList<GameData> gamedataList = new ArrayList<GameData>();

	private GameData gamedata;
	int id;
	int av_id;
	int data_initial;
	int data_ing;
	int data_finish;
	int time_keeping;
	int time_best;
	int state;
	int grade;

	public void setColumnIndex(Cursor cursor) {
		id = cursor.getColumnIndex("id");
		av_id = cursor.getColumnIndex("av_id");
		data_initial = cursor.getColumnIndex("data_initial");
		data_ing = cursor.getColumnIndex("data_ing");
		data_finish = cursor.getColumnIndex("data_finish");
		time_keeping = cursor.getColumnIndex("time_keeping");
		time_best = cursor.getColumnIndex("time_best");
		state = cursor.getColumnIndex("state");
		grade = cursor.getColumnIndex("grade");
	}

	public void setGameData(Cursor cursor) {
		gamedata = new GameData();
		gamedata.setId(cursor.getInt(id));
		gamedata.setAv_id(cursor.getInt(av_id));
		gamedata.setData_initial(cursor.getString(data_initial));
		gamedata.setData_ing(cursor.getString(data_ing));
		gamedata.setData_finish(cursor.getString(data_finish));
		gamedata.setTime_keeping(cursor.getInt(time_keeping));
		gamedata.setTime_best(cursor.getInt(time_best));
		gamedata.setState(cursor.getInt(state));
		gamedata.setGrade(cursor.getInt(grade));
	}

	public GameData getGameDataById(Context context, int id) {
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.Query("select * from gamedata where id="
				+ id);
		setColumnIndex(cursor);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			setGameData(cursor);
		}
		cursor.close();
		gamedataDao.close();
		return gamedata;
	}

	public GameData getGameDataByAVId(Context context, int id) {
		gamedata = new GameData();
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.Query("select * from gamedata where av_id="
				+ id);
		setColumnIndex(cursor);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			setGameData(cursor);
		}
		cursor.close();
		gamedataDao.close();
		return gamedata;
	}

	public ArrayList<GameData> getGameDataList(Context context) {
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.select();
		setColumnIndex(cursor);
		for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
			setGameData(cursor);
			gamedataList.add(gamedata);
		}
		cursor.close();
		gamedataDao.close();
		return gamedataList;
	}

	public List<GameData> getGameDataListAsSQL(Context context, String sql) {
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.Query(sql);
		setColumnIndex(cursor);
		for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
			setGameData(cursor);
			gamedataList.add(gamedata);
		}
		cursor.close();
		gamedataDao.close();
		return gamedataList;
	}

	public String getStrAsSQL(Context context, String sql) {
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.Query(sql);
		String str = null;
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			int columnNameInt = cursor.getColumnIndex("str");
			str = cursor.getString(columnNameInt);
		}
		cursor.close();
		gamedataDao.close();
		return str;
	}

	/**
	 * 
	 * @param context
	 * @param sql
	 *            要求语句类似 select num from XXX 或者select XX as num from XXX
	 * @return
	 */
	public int getNumAsSQL(Context context, String sql) {
		GameDataDao gamedataDao = new GameDataDao(context);
		Cursor cursor = gamedataDao.Query(sql);

		Integer num = null;
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			int columnNameInt = cursor.getColumnIndex("num");
			num = cursor.getInt(columnNameInt);
		}

		cursor.close();
		gamedataDao.close();
		return num;
	}

	public List<GameData> getGameDataListAsStage(Context context, int grade) {
		GameDataDao gamedataDao = new GameDataDao(context);
		String sql = "select * from gamedata where grade=" + grade
				+ " order by av_id";
		Cursor cursor = gamedataDao.Query(sql);
		setColumnIndex(cursor);
		for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
			setGameData(cursor);
			gamedata.avName = "";
			gamedataList.add(gamedata);
		}
		cursor.close();
		gamedataDao.close();
		return gamedataList;
	}

	public void insert(Context context, GameData gamedata) {
		GameDataDao gamedataDao = new GameDataDao(context);
		gamedataDao.insert(gamedata);
		gamedataDao.close();
	}

	public void update(Context context, GameData gamedata) {
		GameDataDao gamedataDao = new GameDataDao(context);
		gamedataDao.update(gamedata);
		gamedataDao.close();
	}

	public void open(Context context, GameData gamedata) {
		gamedata.setState(1);
		update(context, gamedata);
	}

	public void openAll(Context context) {
		Comm.currentGameData.setState(1);
		GameDataDao gamedataDao = new GameDataDao(context);
		gamedataDao.execSQL("update " + gamedataDao.tableName
				+ " set state = 1");
	}
}
